package com.mi.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.query.Query;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.stereotype.Repository;

import com.mi.dao.OrderDao;
import com.mi.entity.Order;
@Repository
public class OrderDaoImp implements OrderDao{
	@Resource
private HibernateTemplate hibernateTemplate;


	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
	this.hibernateTemplate = hibernateTemplate;
}

 
	 
	@Override
	public List<Order> findAll(String pageNumber,String pageSize) {

	//	List<Order> find =  (List<Order>) hibernateTemplate.find("select new com.mi.entity.Order(o.orderNum,o.state,o.money)  from Order o");
		Query query = hibernateTemplate.getSessionFactory().getCurrentSession().getSession().createQuery("select new com.mi.entity.Order(o.id,o.orderNum,o.state,o.money)  from Order o where o.state>0");
		//���÷�ҳ��ʼλ��
		int pageNumber1=Integer.parseInt(pageNumber);
		//���÷�ҳ��С
		int pageSize1=Integer.parseInt(pageSize);
		pageNumber1=(pageNumber1-1)*pageSize1;
		
		query.setFirstResult(pageNumber1);
		query.setMaxResults(pageSize1);
		List list = query.list();
		return list;
	}



	@Override
	public List<Order> findAllByPage(String pageNumber, String pageSize) {
		
		Query query = hibernateTemplate.getSessionFactory().getCurrentSession().getSession().createQuery("select new com.mi.entity.Order(o.id,o.orderNum,o.state,o.money)  from Order o where o.state=0");
		//���÷�ҳ��ʼλ��
		int pageNumber1=Integer.parseInt(pageNumber);
		//���÷�ҳ��С
		int pageSize1=Integer.parseInt(pageSize);
		pageNumber1=(pageNumber1-1)*pageSize1;
		
		query.setFirstResult(pageNumber1);
		query.setMaxResults(pageSize1);
		List<Order> list = query.list();
		 
		return list;
		
	
	}



	@Override
	public String zongJia(String uid) {

		Double zongjia=0.0;
		
		Query query = hibernateTemplate.getSessionFactory().getCurrentSession().getSession().createQuery("select money  from Order o where o.user.id=1 and o.state=0 ");
		 
		List list = query.list();
		for (Object object : list) {
			zongjia+=Double.parseDouble(object.toString());
		}
		 
		return zongjia.toString();
		
	
	
	}
	
	 
	

}
